package com.towexin.application.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.towexin.application.system.domain.Authority;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface AuthorityMapper extends BaseMapper<Authority> {
	// 验证权限的父节点是已关联菜单
	@Select("SELECT m_id FROM t_authority_menu WHERE a_id=#{aId}")
	String checkMenu(String aId);

	// 根据用户名获取权限列表带菜单id与名称
	@Select("SELECT a.*,e.id menu_id,e.menu_name FROM t_authority a LEFT JOIN t_role_authority b ON a.id=b.a_id LEFT JOIN t_role c ON c.id=b.r_id LEFT JOIN t_authority_menu d ON d.a_id=a.id LEFT JOIN t_menu e ON e.id=d.m_id LEFT JOIN t_user_role f ON f.r_id=c.id LEFT JOIN t_user g ON g.id=f.u_id WHERE g.username=#{username} AND c.status=1 ORDER BY a.sort ASC")
	List<Map<String, Object>> selectMapAllByUserName(String username);

	// 根据用户名获取权限列表(授权用)
	@Select("SELECT a.* FROM t_authority a LEFT JOIN t_role_authority b ON a.id=b.a_id LEFT JOIN t_role c ON c.id=b.r_id LEFT JOIN t_user_role d ON c.id=d.r_id LEFT JOIN t_user e ON e.id=d.u_id WHERE e.username=#{username} AND c.status=1")
	List<Authority> selectAuthByUserName(String username);

	@Select("SELECT a.* FROM t_authority a LEFT JOIN t_role_authority b ON a.id = b.a_id WHERE b.r_id = #{rId}")
	List<Authority> selectAuthByRid(String rId);

	// 根据菜单href获取相应权限
	@Select("SELECT a.* FROM t_authority a LEFT JOIN t_authority_menu b ON a.id=b.a_id LEFT JOIN t_menu c ON b.m_id=c.id WHERE c.menu_href=#{href}")
	Authority selectAuthorityByMenuHref(String href);

	@Select("SELECT a.authority_mark FROM t_authority a LEFT JOIN t_role_authority b ON a.id=b.a_id LEFT JOIN t_role c ON c.id=b.r_id LEFT JOIN t_user_role d ON c.id=d.r_id LEFT JOIN t_user e ON e.id=d.u_id WHERE e.username=#{username} AND c.`status` IS TRUE UNION ALL SELECT a.authority_mark FROM t_authority a LEFT JOIN t_role_authority b ON a.id=b.a_id LEFT JOIN t_role c ON c.id=b.r_id WHERE c.default_role IS TRUE")
	List<String> selectMarksAndDefaultRoleByUserName(String username);
}
